package com.xiaogao.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaogao.common.entity.PageData;
import com.xiaogao.sys.mapper.SysLogOperateMapper;
import com.xiaogao.sys.model.SysLogOperate;
import com.xiaogao.sys.request.log.operate.QueryParam;
import com.xiaogao.sys.response.SysLogOperateResponse;
import com.xiaogao.sys.service.SysLogOperateService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@RequiredArgsConstructor
@Service
@Slf4j
public class SysLogOperateServiceImpl  extends ServiceImpl<SysLogOperateMapper, SysLogOperate> implements SysLogOperateService {
    private final SysLogOperateMapper operateMapper;

    @Override
    public PageData<SysLogOperateResponse> querySelective(Integer pageNum, Integer pageSize, QueryParam queryParam) {
        // limit offset,pageSize    ---> offset = (pageNum - 1) * pageNum
        Integer offset = (pageNum - 1) * pageNum;
        List<SysLogOperateResponse> list = operateMapper.querySelective(offset,pageSize,queryParam);

        LambdaQueryWrapper<SysLogOperate> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(queryParam.getHost()),SysLogOperate::getHost,queryParam.getHost());
        wrapper.eq(StringUtils.isNotEmpty(queryParam.getOwnerId()),SysLogOperate::getOwnerId,queryParam.getOwnerId());
        if (queryParam.getRangeTime() != null && queryParam.getRangeTime().size() == 2) {
            wrapper.ge(SysLogOperate::getStartTime, queryParam.getRangeTime().get(0));  // >= 开始时间
            wrapper.le(SysLogOperate::getEndTime, queryParam.getRangeTime().get(1));  // <= 结束时间
        }
        Long total = operateMapper.selectCount(wrapper);
        return new PageData<>(list,total);
    }

    @Override
    public void delete(List<String> ids) {
        if(CollectionUtils.isNotEmpty(ids)){
            operateMapper.deleteByIds(ids);
        }
    }
}
